package net.sunniwell.app.mediaplayer02;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.media3.common.MediaItem;
import androidx.media3.common.Player;
import androidx.media3.exoplayer.ExoPlayer;
import androidx.media3.ui.PlayerView;

//import com.google.android.exoplayer2.ExoPlayer;
//import com.google.android.exoplayer2.Player;
//import com.google.android.exoplayer2.SimpleExoPlayer;
//import com.google.android.exoplayer2.ui.PlayerView;

public class MyPlayerControlView extends Activity {
    private PlayerView playerControlView;
    private ExoPlayer exoPlayer;
    ImageView playButton,pauseButton;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.exo_playview);
        playerControlView = findViewById(R.id.video_view);
        //设置播放器
        exoPlayer = new ExoPlayer.Builder(this).build();
        playerControlView.setPlayer(exoPlayer);
        long currentPosition = exoPlayer.getCurrentPosition();
        exoPlayer.seekTo(currentPosition + 2000);


        //播放
        playerControlView.findViewById(R.id.exo_play).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("sksksksks", "onclick exo_play 播放");
            }
        });


        //快进
        playerControlView.findViewById(R.id.exo_ffwd).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("sksksksks", "onclick exo_ffwd 前进");
                long currentPosition = exoPlayer.getCurrentPosition();
                exoPlayer.seekTo(currentPosition + 2000);
            }
        });


        //后退
        playerControlView.findViewById(R.id.exo_rew).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("sksksksks", "onclick exo_ffwd 前进");
                long currentPosition = exoPlayer.getCurrentPosition();
                exoPlayer.seekTo(currentPosition - 2000);
            }
        });

        //字符串转换成url格式
        String videoLocalPlayUrl = "rawresource:///" + R.raw.local_movie;
        Uri videoUri = Uri.parse(videoLocalPlayUrl);
        MediaItem mediaItem = MediaItem.fromUri(videoUri);

        //资源放进播放器准备播放
        exoPlayer.setPlayWhenReady(true);
        exoPlayer.setMediaItem(mediaItem);

        //准备播放器
        exoPlayer.prepare();
        exoPlayer.setRepeatMode(Player.REPEAT_MODE_ONE);

        //设置播放键和暂停键
        View view= LayoutInflater.from(this).inflate(R.layout.exo_player_control_view,null,false);
        ImageView playButton = view.findViewById(R.id.exo_play);
//        ImageView pauseButton = view.findViewById(R.id.exo_pause);

        //实现播放和暂停键
        playButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(exoPlayer.isPlaying()){
                    exoPlayer.pause();
                    updatePlayPauseButton(true);
                }
                else{
                    exoPlayer.play();
                    updatePlayPauseButton(false);
                }
            }
        });
    }

    /*
    *更新播放，暂停按钮
    * */
    private void updatePlayPauseButton(boolean showPlay) {
        if (showPlay) {
            playButton.setImageResource(R.drawable.play);
        } else {
            playButton.setImageResource(R.drawable.pause);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (exoPlayer != null) {
            exoPlayer.release();
        }
    }
}
